package com.hjq.fcloud.ui.activity

import android.content.Context
import android.content.Intent
import android.view.Gravity
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.flyco.tablayout.listener.OnTabSelectListener
import com.hjq.fcloud.R
import com.hjq.fcloud.app.AppActivity
import com.hjq.fcloud.config.Config
import com.hjq.fcloud.http.api.FinancingOverviewApi
import com.hjq.fcloud.http.model.HttpData
import com.hjq.fcloud.ui.adapter.CommonPagerAdapter
import com.hjq.fcloud.ui.adapter.FinancingOverviewDrawerAdapter
import com.hjq.fcloud.ui.adapter.FinancingOverviewDrawerListener
import com.hjq.fcloud.ui.fragment.FinancingOverviewFragment
import com.hjq.http.EasyHttp
import com.hjq.http.listener.HttpCallback
import kotlinx.android.synthetic.main.activity_financing_overview.drawerLayout
import kotlinx.android.synthetic.main.activity_financing_overview.emptyView
import kotlinx.android.synthetic.main.activity_financing_overview.listView
import kotlinx.android.synthetic.main.activity_financing_overview.tabLayout
import kotlinx.android.synthetic.main.activity_financing_overview.titleBar
import kotlinx.android.synthetic.main.activity_financing_overview.viewPager
import okhttp3.Call

/**
 * 融资需求全览
 */
class FinancingOverviewActivity : AppActivity(), FinancingOverviewDrawerListener {

    companion object {
        const val INTENT_IN_FUC_ID = "fnc_id"
        fun start(context: Context, fnc_id: String) {
            var intent = Intent(context, FinancingOverviewActivity::class.java)
            intent.putExtra(INTENT_IN_FUC_ID, fnc_id)
            context.startActivity(intent)
        }
    }

    val fncId by lazy { getString(INTENT_IN_FUC_ID) }
    var mDrawerAdapter: FinancingOverviewDrawerAdapter? = null

    override fun getLayoutId(): Int {
        return R.layout.activity_financing_overview
    }

    override fun initView() {
        titleBar.rightView.setOnClickListener {
            drawerLayout.openDrawer(Gravity.LEFT);
        }
        emptyView.setOnClickListener {
            drawerLayout.closeDrawers();
        }
    }

    override fun initData() {
        getServerFlow()
    }

    private fun getServerFlow() {
        var fnc_id: String? = ""
        if (Config.IS_TEST) {
            //TODO:写死的fnc_id 88
            fnc_id = "88"
        } else {
            fnc_id = fncId
        }
        showDialog()
        var api = FinancingOverviewApi()
            .setFnc_Id(fnc_id)
        EasyHttp.post(this)
            .api(api)
            .request(object :
                HttpCallback<HttpData<FinancingOverviewApi.DataRootJson>>(this) {
                override fun onSucceed(result: HttpData<FinancingOverviewApi.DataRootJson>) {
                    result.getData()?.let { data ->
                        var list = data.flow!!
                        setFragmentAdapter(list)
                        setDrawerAdapter(list)
                    }
                }

                override fun onEnd(call: Call?) {
                    super.onEnd(call)
                    hideDialog()
                }
            })
    }

    private fun setDrawerAdapter(list: ArrayList<FinancingOverviewApi.DataJson>) {
        mDrawerAdapter =
            FinancingOverviewDrawerAdapter(context, list)
        mDrawerAdapter?.setFinancingOverviewDrawerListener(this)
        listView.adapter = mDrawerAdapter
    }

    private fun setFragmentAdapter(list: ArrayList<FinancingOverviewApi.DataJson>) {
        val fragments = ArrayList<Fragment>()
        var pagerAdapterList = ArrayList<CommonPagerAdapter.PagerAdapterBean>()
        list.filter {
            it.enable?.equals("1") == true
        }.forEach {
            var title = it.info?.trim() ?: ""
            var item = CommonPagerAdapter.PagerAdapterBean(title)
            pagerAdapterList.add(item)
            fragments.add(FinancingOverviewFragment.newInstance(title))
        }
        val mAdapter = CommonPagerAdapter(supportFragmentManager, pagerAdapterList, fragments)
        viewPager.setAdapter(mAdapter)
        viewPager.setOffscreenPageLimit(pagerAdapterList.size)
        viewPager.addOnPageChangeListener(object : OnPageChangeListener {
            override fun onPageScrolled(p0: Int, p1: Float, p2: Int) {
            }

            override fun onPageSelected(position: Int) {
                selectDrawer(position)
            }

            override fun onPageScrollStateChanged(p0: Int) {
            }
        })
        tabLayout.setViewPager(viewPager)
        tabLayout.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                selectDrawer(position)
            }

            override fun onTabReselect(p0: Int) {
            }
        })
    }

    private fun selectDrawer(position: Int) {
        listView.setSelection(position)
        //post的意义是等listView.setSelection(position)执行完成再刷新适配器，要不然没效果
        listView.post { mDrawerAdapter?.setSelection(position) }
    }

    override fun onSelect(index: Int, item: Any) {
        drawerLayout.closeDrawers();
        tabLayout.currentTab = index
    }
}